<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-tabs>
        <!-- 角色管理页 -->
        <el-tab-pane label="角色管理">
          <el-row>
            <el-button icon="el-icon-plus" type="primary" size="small" @click="showDialog=true">新增角色</el-button>
          </el-row>
          <!-- 表格 -->
          <el-table :border="true" :data="list">
            <el-table-column label="序号" width="120" type="index" />
            <el-table-column label="角色名" width="240" prop="name" />
            <el-table-column label="描述" prop="description" />
            <el-table-column label="操作">
              <template slot-scope="{row}">
                <el-button type="primary" size="small" @click="assignPerm(row.id)">分配权限</el-button>
                <el-button type="primary" size="small" @click="editRole(row.id)">修改</el-button>
                <el-button type="danger" size="small" @click="deleteRole(row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <el-row type="flex" justify="center" align="middele" style="height:60px">
            <el-pagination
              layout="prev, pager, next"
              :current-page="page.page"
              :page-size="page.pagesize"
              :total="page.total"
              @current-change="changePage"
            />
          </el-row>
        </el-tab-pane>

        <!-- 公司信息页面 -->
        <el-tab-pane label="公司信息">
          <el-alert
            title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
            type="info"
            :closable="false"
            :show-icon="true"
          />
          <el-form label-width="120px" style="margin-top:20px">
            <el-form-item label="企业名称">
              <el-input v-model="formData.name" disabled style="width:400px" />
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input v-model="formData.companyAddress" disabled style="width:400px" />
            </el-form-item>
            <el-form-item label="公司电话">
              <el-input v-model="formData.companyPhone" disabled style="width:400px" />
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="formData.mailbox" disabled style="width:400px" />
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="formData.remarks" disabled style="width:400px" />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
      <!-- 弹层 -->
      <el-dialog title="编辑角色" :visible="showDialog" @close="btnCancel">
        <el-form ref="roleForm" :model="roleForm" label-width="120px" :rules="rules">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" autocomplete="off" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" autocomplete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button size="small" @click="btnCancel">取 消</el-button>
          <el-button type="primary" size="small" @click="btnOK">确 定</el-button>
        </div>
      </el-dialog>

    </div>
    <!-- 分配角色弹层 -->
    <el-dialog title="分配角色" :visible.sync="showPermDialog" @close="btnPermCancel">
      <!-- 树形结构 -->
      <el-tree
        ref="permTree"
        :data="permData"
        :props="defaultProps"
        :show-checkbox="true"
        :check-strictly="true"
        :default-expand-all="true"
        :default-checked-keys="selectCheck"
        node-key="id"
      />
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
          <el-button size="small" @click="btnPermCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, deleteRole, getRoleDetail, updateRole, addRole, assignPerm } from '@/api/setting'
import { mapGetters } from 'vuex'
import { getPermissionList } from '@/api/permission' // 获取所有权限点
import { tranListToTreeData } from '@/utils'
export default {
  // 组件名称
  name: 'Setting',
  // 局部注册的组件
  components: {},
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data () {
    return {
      showPermDialog: false, // 分配弹层
      defaultProps: {
        label: 'name'
      },
      permData: [], // 专门用来接收权限数据 树形数据
      selectCheck: [], // 定义一个数组来接收 已经选中的节点
      roleId: null, // 用来记录分配角色的id
      showDialog: false, // 关闭弹层
      roleForm: {
        name: '',
        description: ''
      }, // 专门接收新增或编辑的表单数据
      rules: {
        name: [
          { required: 'true', trigger: 'blur', message: '角色名称不能为空' }
        ]
      },
      list: [], // 角色列表数据
      page: {
        page: 1, // 页码
        pagesize: 10, // 每页条数
        total: 0 // 总数据
      },
      formData: {
        name: '',
        companyAddress: '',
        companyPhone: '',
        mailbox: '',
        remarks: ''
      }

    }
  },
  // 计算属性
  computed: {
    ...mapGetters(['companyId'])
  },
  // 侦听器
  watch: {},
  // 生命周期钩子   注：没用到的钩子请自行删除
  /**
  * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
  */
  created () {
    this.getRoleList() // 获取角色列表
    this.getCompanyInfo() // 根据id获取企业
  },
  /**
  * el 被新创建的 vm.el 替换，并挂载到实例上去之后调用该钩子。
  * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.el 也在文档内。
  */
  mounted () {},
  // 组件方法
  methods: {
    async getRoleList () {
      // 获取角色
      const { total, rows } = await getRoleList(this.page)
      // console.log(result)
      this.page.total = total
      this.list = rows
    },
    changePage (newpage) {
      this.page.page = newpage
      this.getRoleList()
    },
    async getCompanyInfo () {
      this.formData = await getCompanyInfo(this.companyId)
      // console.log(result)
    },
    async deleteRole (id) {
      try {
        await this.$confirm('确认删除此角色吗？', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await deleteRole(id)
        this.getRoleList()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    },
    async editRole (id) {
      this.roleForm = await getRoleDetail(id)
      this.showDialog = true
    },
    async btnOK () {
      try {
        await this.$refs.roleForm.validate()

        if (this.roleForm.id) {
          // 编辑
          await updateRole(this.roleForm)
          // alert('校验成功')
        } else {
          // 新增
          await addRole(this.roleForm)
        }
        this.getRoleList() // 重新加载数据
        this.$message.success('操作成功')
        this.showDialog = false
      } catch (error) {
        // alert('校验失败')
        console.log(error)
      }
    },
    btnCancel () {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
      this.showDialog = false
    },
    // 分配权限
    async assignPerm (id) {
      this.permData = tranListToTreeData(await getPermissionList(), '0')
      this.roleId = id
      const { permIds } = await getRoleDetail(id)
      this.selectCheck = permIds
      this.showPermDialog = true
    },
    // 分配弹层确定
    async btnPermOK () {
      await assignPerm({ permIds: this.$refs.permTree.getCheckedKeys(), id: this.roleId })
      this.$message.success('操作成功')
      this.showPermDialog = false
    },
    // 分配弹层取消
    btnPermCancel () {
      this.selectCheck = []
      this.showPermDialog = false
    }
  }
}
</script>

<style scoped lang="less">

</style>

